<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$village_id = formatInput( $_POST[ 'village_id' ] );
$army_id = formatInput( $_POST[ 'army_id' ] );
$village_name = formatInput( $_POST[ 'village_name' ] );
$query = "SELECT ruler_id, name, scout, infantry, cavalry, artillery, officer_id, x, y, arrived_turn " .
		"FROM army " .
		"WHERE army_id = " . $army_id;
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$name = $row[ 'name' ];
$arrived_turn = $row[ 'arrived_turn' ];
$x = $row[ 'x' ];
$y = $row[ 'y' ];
$turn = getCurrentTurn();
$scoutBefore = $scout = $row[ 'scout' ];
$infantryBefore = $infantry = $row[ 'infantry' ];
$cavalryBefore = $cavalry = $row[ 'cavalry' ];
$artilleryBefore = $artillery = $row[ 'artillery' ];
if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	global $language;
	$officerId = $row[ 'officer_id' ];
	$query = "SELECT name, persuasion " .
			"FROM officers " .
			"WHERE officer_id = " . $row[ 'officer_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$officerName = $row[ 'name' ];
	$persusasion = $row[ 'persuasion' ];
	
	$query = "SELECT count(*) " .
			"FROM village " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$total_villages = $row[ 'count(*)' ];
	
	$query = "SELECT turns_inactive " .
			"FROM game";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$turns_to_inactive = $row[ 'turns_inactive' ];
	
	$query4 = "SELECT * " .
			"FROM village " .
			"WHERE x = $x AND y = $y";
	$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
	$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
	$capital = false;
	$no_attack = false;
	$old_name = $row4[ 'name' ];
	if( isset( $row4[ 'start_place' ] ) && $row4[ 'start_place' ] == 1 && $row4[ 'ruler_id' ] != -1 ) {
		$query5 = "SELECT resigned, gold, iron, coal, active_turn " .
				"FROM ruler " .
				"WHERE ruler_id = " . $row4[ 'ruler_id' ];
		$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
		$row5 = mysql_fetch_array( $result5, MYSQL_ASSOC );
		if( ( $row5[ 'resigned' ] == 1 || $row5[ 'active_turn' ] + $turns_to_inactive < $turn ) && $row5[ 'gold' ] < 500 && $row5[ 'iron' ] < 1000 && $row5[ 'coal' ] < 1500 ) {
			$query5 = "SELECT count(*) " .
					"FROM village " .
					"WHERE ruler_id = " . $row4[ 'ruler_id' ];
			$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
			$row5 = mysql_fetch_array( $result5, MYSQL_ASSOC );
			if( $row5[ 'count(*)' ] > 1 ) {
				$capital = true;
			}
		}
		else {
			$capital = true;
		}
	}
	elseif( isset( $row4[ 'start_place' ] ) && $row4[ 'ruler_id' ] == -1 ) {
		$no_attack = true;
	}

	if( $total_villages < getVillageLimit() || ( $capital && !$no_attack ) ) {
		if( checkRangeVillage( $village_id ) ) {
			$blocked = false;
			$query2 = "SELECT ruler_id " .
					"FROM army " .
					"WHERE x = " . $x . " AND y = " . $y . " AND travel = 0 AND scout + infantry + cavalry + artillery > 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$num = mysql_numrows( $result2 );
			$i = 0;
			while ( $i < $num ) {
				if( $_SESSION[ 'ruler_id' ] != mysql_result( $result2, $i, "ruler_id" ) ) {
					$query3 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
					$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
					$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
					if( isset( $row3[ 'faction_id' ] ) ) {
						$tmp_faction_id = $row3[ 'faction_id' ];
						$query3 = "SELECT faction_id " .
								"FROM faction_member " .
								"WHERE ruler_id = " . mysql_result( $result2, $i, "ruler_id" ) . " AND requested = 0 AND invited = 0";
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
						if( isset( $row3[ 'faction_id' ] ) && $row3[ 'faction_id' ] != $tmp_faction_id ) {
							$blocked = true;
						}
					}
					else {
						$blocked = true;
					}
				}
				$i ++;
			}
			
			if( !$blocked ) {
				$query = "SELECT name FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				$rulerName = $row[ 'name' ];
					
				$query = "SELECT count(*) FROM village WHERE name = '" . $village_name . "' AND village_id != $village_id";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				if( $village_name == "" && !$capital ) {
					$_SESSION[ 'conquer_village_status' ] = translate( "You need to fill in a new village name." );		
					header( "Location: army.php?name=$name" );
				}
				elseif( $row[ 'count(*)' ] > 0 ) {
					$_SESSION[ 'conquer_village_status' ] = translate( "A village with that name already exists." );		
					header( "Location: army.php?name=$name" );	
				}
				else {
					$query = "SELECT population, x, y, ruler_id, name FROM village WHERE village_id = " . $village_id;
					$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					$row = mysql_fetch_array( $result, MYSQL_ASSOC );
					
					$query2 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
					if( isset( $row2[ 'faction_id' ] ) ) {
						$query3 = "SELECT faction.faction_id, nationalism FROM faction_member " .
								"JOIN faction ON faction.faction_id = faction_member.faction_id " .
								"WHERE ruler_id = " . $row[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
						$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
						$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
						if( isset( $row3[ 'nationalism' ] ) && $row3[ 'nationalism' ] == 1 ) {
							$nationalism = true;
						}
						if( isset( $row3[ 'faction_id' ] ) && $row2[ 'faction_id' ] == $row3[ 'faction_id' ] ) {
							$_SESSION[ 'conquer_village_status' ] = translate( "This village belongs to a faction member." );
							$continue = false;
						}
						else {
							$continue = true;
						}
					}
					else {
						$continue = true;
					}
					
					if( $continue ) {
						$village_ruler = $row[ 'ruler_id' ];
						$population = $row[ 'population' ];
						if( isset( $nationalism ) ) {
							$enemies = $population * 0.25;
						}
						else {
							$enemies = $population * 0.10;
						}
						
						if( $capital ) {
							$enemies *= 2;
						}
						
						// randomize a bit
						$enemies = $enemies * 0.975 + rand( 0, $enemies * 0.05 );
						if( $capital && $population - $enemies < 2500 ) {
							$enemies = $population - 2500;
							if( $enemies < 0 ) {
								$enemies = 0;
							}
						}
						$x = $row[ 'x' ];
						$y = $row[ 'y' ];
						$location = $row[ 'name' ] . " (" . $x . ", " . $y . ")";
						
						$damage_village = round( ( $enemies * 0.8 ) * ( 1 - 0.1 * $persusasion ) );
						$damage_army = $scout * 2 + $infantry * 3 + $cavalry * 15 + $artillery * 24;
						$enemies_after = ( int ) ( $enemies - $damage_army );
						if( $enemies_after < 0 ) {
							$enemies_after = 0;
						}
						
						$scoutsKilled = $scout;
						$infantryKilled = $infantry;
						$cavalryKilled = $cavalry;
						$artilleryKilled = $artillery;
												
						$count = 0;
						$add = ( int ) ( $damage_village * 0.075 );
						while( $add > 0 && $count < 5 ) {
							$scout -= $add;
							if( $scout < 0 ) {
								$add = -$scout * 0.7;
								$scout = 0;
							}
							else {
								$add = 0;
							}
							if( $count == 0 ) {
								$add += ( int ) ( $damage_village * 0.08 );
							}
							$infantry -=  $add;
							if( $infantry < 0 ) {
								$add = -$infantry * 0.19;
								$infantry = 0;
							}
							else {
								$add = 0;
							}
							if( $count == 0 ) {
								$add += ( int ) ( $damage_village * 0.016 );
							}
							$cavalry -= $add;
							if( $cavalry < 0 ) {
								$add = -$cavalry * 0.65;
								$cavalry = 0;
							}
							else {
								$add = 0;
							}
							if( $count == 0 ) {
								$add += ( int ) ( $damage_village * 0.01 );
							}
							$artillery -= $add;
							if( $artillery < 0 ) {
								$add = -$artillery * 12;
								$artillery = 0;
							}
							else {
								$add = 0;
							}
							$count ++;
						}
						
						$scoutsKilled -= $scout;
						$infantryKilled -= $infantry;
						$cavalryKilled -= $cavalry;
						$artilleryKilled -= $artillery;
						$experienceGained = $scoutsKilled * 32 + $infantryKilled * 48 + $cavalryKilled * 256 + $artilleryKilled * 400;

						$query4 = "UPDATE ruler " .
								"SET experience = experience + " . $experienceGained . ", scouts_killed = scouts_killed + $scoutsKilled, infantry_killed = infantry_killed + $infantryKilled, " .
								"cavalry_killed = cavalry_killed + $cavalryKilled, artillery_killed = artillery_killed + $artilleryKilled " .
								"WHERE ruler_id = $village_ruler";
						mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
						
						$query4 = "UPDATE ruler " .
								"SET experience = experience + " . round( $experienceGained * 0.5 ) . ", scouts_lost = scouts_lost + $scoutsKilled, infantry_lost = infantry_lost + $infantryKilled, " .
								"cavalry_lost = cavalry_lost + $cavalryKilled, artillery_lost = artillery_lost + $artilleryKilled " .
								"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
						mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
						
						$query4 = "UPDATE officers " .
								"SET experience = experience + " . round( $experienceGained * 0.5 ) . " " .
								"WHERE officer_id = " . $officerId;
						mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
						
						$here_for = -1;
						if( $arrived_turn != -1 ) {
							$here_for = $turn - $arrived_turn;
						}
						
						$query4 = "SELECT language " .
								"FROM users " .
								"JOIN ruler ON ruler.user_id = users.user_id " .
								"WHERE ruler_id = " . $village_ruler;
						$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
						$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
						$village_language = $row4[ 'language' ];
						$attacker_language = $language;
						
						if( $capital && $here_for >= 12 ) {
							$subject = translate( "Loot report for" ) . " $location";
							$language = $village_language;
							$subject2 = translate( "Loot report for" ) . " $location";
							$language = $attacker_language;
							$end = "";
							if( $scout + $infantry + $cavalry + $artillery > 0 && $enemies_after == 0 ) {
								$query4 = "SELECT turns_inactive " .
										"FROM game";
								$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
								$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
								$turns_to_inactive = $row4[ 'turns_inactive' ];
								
								$start = translate( "We looted the village at" ) . " $location!\r\n\r\n" . translate( "Battle overview" ) . ":\r\n";
								$language = $village_language;
								$start2 = translate( "We looted the village at" ) . " $location!\r\n\r\n" . translate( "Battle overview" ) . ":\r\n";
								$language = $attacker_language;

								$query = "UPDATE army " .
										"SET scout = " . $scout . ", infantry = " . $infantry . ", cavalry = " . $cavalry . ", artillery = " . $artillery . " " .
										"WHERE army_id = " . $army_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE village " .
										"SET population = " . ( $population - $enemies ) . " " .
										"WHERE village_id = " . $village_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								
								$query = "UPDATE army " .
										"SET arrived_turn = $turn " .
										"WHERE x = $x AND y = $y";
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								
								$loot = true;
								$query4 = "SELECT coal, iron, gold, resigned, active_turn FROM ruler WHERE ruler_id = " . $row[ 'ruler_id' ];
								$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
								$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
								$gold = $row4[ 'gold' ];
								$iron = $row4[ 'iron' ];
								$coal = $row4[ 'coal' ];
								$modifier = 30000;
								if( $row4[ 'resigned' ] == 1 || $row4[ 'active_turn' ] + $turns_to_inactive < $turn ) {
									$modifier = 0;
								}
								
								$stolen_gold = 0;
								$stolen_iron = 0;
								$stolen_coal = 0;
								
								if( $gold > $modifier ) {
									if( $modifier == 0 ) {
										$stolen_gold = $gold;
									}
									else {
										$stolen_gold = $gold * 0.3;
									}
								}
								if( $iron > $modifier * 2.5 ) {
									if( $modifier == 0 ) {
										$stolen_iron = $iron;
									}
									else {
										$stolen_iron = $iron * 0.3;
									}
								}
								if( $coal > $modifier * 5 ) {
									if( $modifier == 0 ) {
										$stolen_coal = $coal;
									}
									else {
										$stolen_coal = $coal * 0.3;
									}
								}
								
								$end = "\r\n";
								$end .= translate( "Resources stolen" ) . ":\r\n";
								$end .= translate( "Gold" ) . ": <strong class=\\\"gold\\\">" . number_format( $stolen_gold ) . "</strong>\r\n";
								$end .= translate( "Iron" ) . ": <strong class=\\\"iron\\\">" . number_format( $stolen_iron ) . "</strong>\r\n";
								$end .= translate( "Coal" ) . ": <strong class=\\\"coal\\\">" . number_format( $stolen_coal ) . "</strong>\r\n";
								$language = $village_language;
								$end2 = "\r\n";
								$end2 .= translate( "Resources stolen" ) . ":\r\n";
								$end2 .= translate( "Gold" ) . ": <strong class=\\\"gold\\\">" . number_format( $stolen_gold ) . "</strong>\r\n";
								$end2 .= translate( "Iron" ) . ": <strong class=\\\"iron\\\">" . number_format( $stolen_iron ) . "</strong>\r\n";
								$end2 .= translate( "Coal" ) . ": <strong class=\\\"coal\\\">" . number_format( $stolen_coal ) . "</strong>\r\n";
								$language = $attacker_language;
								
								$query = "UPDATE ruler " .
										"SET gold = gold - $stolen_gold, " .
											"iron = iron - $stolen_iron, " .
											"coal = coal - $stolen_coal, " .
											"morale = morale - 0.3 " .
										"WHERE ruler_id = " . $row[ 'ruler_id' ];
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE ruler SET gold = gold + $stolen_gold, iron = iron + $stolen_iron, coal = coal + $stolen_coal " .
										"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$_SESSION[ 'conquer_village_status' ] = translate( "Capital successfully looted." );
								
							}
							elseif( $enemies_after > 0 && $scout + $infantry + $cavalry + $artillery > 0 ) {
								$start = translate( "We were defeated trying to loot the capital at" ) . " $location!\r\n" . translate( "Battle overview" ) . ":\r\n";
								$language = $village_language;
								$start2 = translate( "We were defeated trying to loot the capital at" ) . " $location!\r\n" . translate( "Battle overview" ) . ":\r\n";
								$language = $attacker_language;
								$query = "UPDATE army SET scout = " . $scout . ", infantry = " . $infantry . ", cavalry = " . $cavalry . ", artillery = " . $artillery . " WHERE army_id = " . $army_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE village SET population = " . ( $population - ( $enemies - $enemies_after ) ) . " WHERE village_id = " . $village_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$loot = false;
								$_SESSION[ 'conquer_village_status' ] = translate( "We failed to loot this capital." );
							}
							else {
								$start = translate( "Our army was killed when it attemted to loot" ) . " $location!\r\n" . translate( "Battle overview" ) . ":\r\n";
								$language = $village_language;
								$start = translate( "Our army was killed when it attemted to loot" ) . " $location!\r\n" . translate( "Battle overview" ) . ":\r\n";
								$language = $attacker_language;
								$query = "DELETE FROM army WHERE army_id = " . $army_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE village SET population = " . ( $population - ( $enemies - $enemies_after ) ) . " WHERE village_id = " . $village_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$loot = false;
								$army_gone = true;
							}
							
							$report = "";
							$report .= $name . " " . translate( 'lead by' ) . " " . $officerName . ' ' . translate( 'under command of' ) . ' ' . $rulerName . "\r\n";
							$report .= translate( 'Scouts before' ) . ': ' . number_format( $scoutBefore ) . "\r\n";
							$report .= translate( 'Scouts after' ) . ': ' . number_format( $scout ) . "\r\n";
							$report .= translate( 'Infantry before' ) . ': ' . number_format( $infantryBefore ) . "\r\n";
							$report .= translate( 'Infantry after' ) . ': ' . number_format( $infantry ) . "\r\n";
							$report .= translate( 'Cavalry before' ) . ': ' . number_format( $cavalryBefore ) . "\r\n";
							$report .= translate( 'Cavalry after' ) . ': ' . number_format( $cavalry ) . "\r\n";
							$report .= translate( 'Artillery before' ) . ': ' . number_format( $artilleryBefore ) . "\r\n";
							$report .= translate( 'Artillery after' ) . ': ' . number_format( $artillery ) . "\r\n\r\n";
					
							$report = $report . translate( 'Capital resistance at' ) . " $location\r\n";
							$report = $report . translate( 'Capital population' ) . ': ' . number_format( ( int ) ( $population ) ) . "\r\n";
							$report = $report . translate( 'Opposing villagers before' ) . ': ' . number_format( ( int ) ( $enemies ) ) . "\r\n";
							$report = $report . translate( 'Opposing villagers after' ) . ': ' . number_format( ( int ) ( $enemies_after ) ) . "\r\n";
							
							$report .= $end;

							$language = $village_language;
							$report2 = "";
							$report2 .= $name . " " . translate( 'lead by' ) . " " . $officerName . ' ' . translate( 'under command of' ) . ' ' . $rulerName . "\r\n";
							$report2 .= translate( 'Scouts before' ) . ': ' . number_format( $scoutBefore ) . "\r\n";
							$report2 .= translate( 'Scouts after' ) . ': ' . number_format( $scout ) . "\r\n";
							$report2 .= translate( 'Infantry before' ) . ': ' . number_format( $infantryBefore ) . "\r\n";
							$report2 .= translate( 'Infantry after' ) . ': ' . number_format( $infantry ) . "\r\n";
							$report2 .= translate( 'Cavalry before' ) . ': ' . number_format( $cavalryBefore ) . "\r\n";
							$report2 .= translate( 'Cavalry after' ) . ': ' . number_format( $cavalry ) . "\r\n";
							$report2 .= translate( 'Artillery before' ) . ': ' . number_format( $artilleryBefore ) . "\r\n";
							$report2 .= translate( 'Artillery after' ) . ': ' . number_format( $artillery ) . "\r\n\r\n";
					
							$report2 = $report2 . translate( 'Capital resistance at' ) . " $location\r\n";
							$report2 = $report2 . translate( 'Capital population' ) . ': ' . number_format( ( int ) ( $population ) ) . "\r\n";
							$report2 = $report2 . translate( 'Opposing villagers before' ) . ': ' . number_format( ( int ) ( $enemies ) ) . "\r\n";
							$report2 = $report2 . translate( 'Opposing villagers after' ) . ': ' . number_format( ( int ) ( $enemies_after ) ) . "\r\n";
							
							$report2 .= $end2;
							$language = $attacker_language;
					
							$date = getCurrentDate();
					
							$query = "INSERT INTO news ( ruler_id, news, subject, type, date, turn ) " .
									"VALUES ( " . $_SESSION[ 'ruler_id' ] . ", \"" . $start . $report . "\", \"$subject\", 2, '$date', $turn )";
							mysql_query( $query ) or die( "Query failed : " . mysql_error() );
							if( $village_ruler != -1 ) {
								$language = $village_language;
								if( $loot ) {
									$start2 = translate( 'Our capital' ) . ", $location, " . translate( 'has been looted' ) . ".\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								}
								else {
									$start2 = translate( 'Our capital' ) . ", $location, " . translate( 'was attacked, but villagers repelled the attack' ) . ".\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								}
								$language = $attacker_language;
								$query = "INSERT INTO news ( ruler_id, news, subject, type, date, turn ) " .
										"VALUES ( " . $village_ruler . ", \"" . $start2 . $report2 . "\", \"$subject2\", 2, '$date', $turn )";
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
							}
							
							if( isset( $army_gone ) ) {
								header( "Location: ../armies/armies.php" );
							}
							else {
								header( "Location: army.php?name=$name" );
							}
						}
						elseif( $here_for >= 0 && !$capital ) {
							$subject = translate( 'Village conquer report' ) . ", $location";
							$language = $village_language;
							$subject2 = translate( 'Village conquer report' ) . ", $location";
							$language = $attacker_language;

							$end = "";
							$end2 = "";
							if( $scout + $infantry + $cavalry + $artillery > 0 && $enemies_after == 0 ) {
								$start = translate( "We took the village at" ) . " $location!\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								$language = $village_language;
								$start2 = translate( "We took the village at" ) . " $location!\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								$language = $attacker_language;
								$query = "UPDATE army " .
										"SET scout = " . $scout . ", infantry = " . $infantry . ", cavalry = " . $cavalry . ", artillery = " . $artillery . " " .
										"WHERE army_id = " . $army_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE village " .
										"SET ruler_id = " . $_SESSION[ 'ruler_id' ] . ", start_place = 0, name = '" . $village_name . "', second_name = '$old_name', " .
											"population = " . ( $population - $enemies ) . ", become_capital = -1, hide = 0, " .
											"loyalty = " . ( ( $village_ruler == -1 ) ? "50 " : "( loyalty - 2 ) " ) . ", `max_population` = $population, `militia` = $turn " .
										"WHERE village_id = " . $village_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE village_names " .
										"SET used = 1 " .
										"WHERE name LIKE '$village_name'";
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$capture = true;
								if( $old_name != $village_name ) {
									$end = "\r\n\r\n" . translate( 'We renamed the village to' ) . ": $village_name.";
									$language = $village_language;
									$end2 = "\r\n\r\n" . translate( 'We renamed the village to' ) . ": $village_name.";
									$language = $attacker_language;
								}
							}
							elseif( $enemies_after > 0 && $scout + $infantry + $cavalry + $artillery > 0 ) {
								$start = translate( "We were defeated trying to take the village at" ) . " $location!\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								$language = $village_language;
								$start2 = translate( "We were defeated trying to take the village at" ) . " $location!\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								$language = $attacker_language;
								$query = "UPDATE army SET scout = " . $scout . ", infantry = " . $infantry . ", cavalry = " . $cavalry . ", artillery = " . $artillery . " WHERE army_id = " . $army_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE village SET population = " . ( $population - ( $enemies - $enemies_after ) ) . " WHERE village_id = " . $village_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$capture = false;
							}
							else {
								$start = translate( "Everyone died during the attemted conquest of" ) . " $location!\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								$language = $village_language;
								$start2 = translate( "Everyone died during the attemted conquest of" ) . " $location!\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								$language = $attacker_language;
								$query = "DELETE FROM army WHERE army_id = " . $army_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$query = "UPDATE village SET population = " . ( $population - ( $enemies - $enemies_after ) ) . " WHERE village_id = " . $village_id;
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
								$capture = false;
							}
							
							$report = "";
					
							$report .= $name . ' ' . translate( 'lead by' ) . ' ' . $officerName . ' ' . translate( 'under command of' ) . ' ' . $rulerName . "\r\n";
							$report .= translate( 'Scouts before' ) . ': ' . number_format( $scoutBefore ) . "\r\n";
							$report .= translate( 'Scouts after' ) . ': ' . number_format( $scout ) . "\r\n";
							$report .= translate( 'Infantry before' ) . ': ' . number_format( $infantryBefore ) . "\r\n";
							$report .= translate( 'Infantry after' ) . ': ' . number_format( $infantry ) . "\r\n";
							$report .= translate( 'Cavalry before' ) . ': ' . number_format( $cavalryBefore ) . "\r\n";
							$report .= translate( 'Cavalry after' ) . ': ' . number_format( $cavalry ) . "\r\n";
							$report .= translate( 'Artillery before' ) . ': ' . number_format( $artilleryBefore ) . "\r\n";
							$report .= translate( 'Artillery after' ) . ': ' . number_format( $artillery ) . "\r\n\r\n";
					
							$report .= translate( 'Village resistance at' ) . " $location\r\n";
							$report .= translate( 'Village population' ) . ': ' . number_format( ( int ) ( $population ) ) . "\r\n";
							$report .= translate( 'Opposing villagers before' ) . ': ' . number_format( ( int ) ( $enemies ) ) . "\r\n";
							$report .= translate( 'Opposing villagers after' ) . ': ' . number_format( ( int ) ( $enemies_after ) ) . "\r\n";
					
							$language = $village_language;
							
							$report2 = "";
					
							$report2 .= $name . ' ' . translate( 'lead by' ) . ' ' . $officerName . ' ' . translate( 'under command of' ) . ' ' . $rulerName . "\r\n";
							$report2 .= translate( 'Scouts before' ) . ': ' . number_format( $scoutBefore ) . "\r\n";
							$report2 .= translate( 'Scouts after' ) . ': ' . number_format( $scout ) . "\r\n";
							$report2 .= translate( 'Infantry before' ) . ': ' . number_format( $infantryBefore ) . "\r\n";
							$report2 .= translate( 'Infantry after' ) . ': ' . number_format( $infantry ) . "\r\n";
							$report2 .= translate( 'Cavalry before' ) . ': ' . number_format( $cavalryBefore ) . "\r\n";
							$report2 .= translate( 'Cavalry after' ) . ': ' . number_format( $cavalry ) . "\r\n";
							$report2 .= translate( 'Artillery before' ) . ': ' . number_format( $artilleryBefore ) . "\r\n";
							$report2 .= translate( 'Artillery after' ) . ': ' . number_format( $artillery ) . "\r\n\r\n";
					
							$report2 .= translate( 'Village resistance at' ) . " $location\r\n";
							$report2 .= translate( 'Village population' ) . ': ' . number_format( ( int ) ( $population ) ) . "\r\n";
							$report2 .= translate( 'Opposing villagers before' ) . ': ' . number_format( ( int ) ( $enemies ) ) . "\r\n";
							$report2 .= translate( 'Opposing villagers after' ) . ': ' . number_format( ( int ) ( $enemies_after ) ) . "\r\n";
							$language = $attacker_language;
							$date = getCurrentDate();
					
							$query = "INSERT INTO news ( ruler_id, news, subject, type, date, turn ) " .
									"VALUES ( " . $_SESSION[ 'ruler_id' ] . ", \"" . $start . $report . $end . "\", \"$subject\", 2, '$date', $turn )";
							mysql_query( $query ) or die( "Query failed : " . mysql_error() );
							
							if( $village_ruler != -1 ) {
								$language = $village_language;
								if( $capture ) {
									$start2 = translate( "Our village at" ) . " $location " . translate( "has been conquered" ) . ".\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								}
								else {
									$start2 = translate( "Our village at" ) . " $location " . translate( "was attacked, but villagers repelled the attack" ) . ".\r\n\r\n" . translate( 'Battle overview' ) . ":\r\n";
								}
								$language = $attacker_language;
					
								$query = "INSERT INTO news ( ruler_id, news, subject, type, date, turn ) " .
										"VALUES ( " . $village_ruler . ", \"" . $start2 . $report2 . "\", \"$subject2\", 2, '$date', $turn )";
								mysql_query( $query ) or die( "Query failed : " . mysql_error() );
							}
							
							if( $capture ) {
								header( "Location: ../villages/village.php?name=$village_name" );
							}
							else {
								header( "Location: ../armies/armies.php" );
							}
						}
					}
					else {
						header( "Location: army.php?name=$name" );
					}
				}
			}
			else {
				$_SESSION[ 'conquer_village_status' ] = translate( "There is a battle going on currently." );
				header( "Location: army.php?name=$name" );
			}
		}
		else {
			$_SESSION[ 'conquer_village_status' ] = translate( "This village is out of range." );
			header( "Location: army.php?name=$name" );
		}
	}
	else {
		if( $no_attack ) {
			$_SESSION[ 'conquer_village_status' ] = translate( "You can't loot this village at the moment." );
		}
		else {
			$_SESSION[ 'conquer_village_status' ] = translate( "You can't take any more villages at the moment." );
		}
		header( "Location: army.php?name=$name" );
	}
}
else {
	header( "Location: army.php?name=$name" );
}
?>